<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会员充值</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form" lay-filter="recharge-form" style="padding: 20px 30px 0 0;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">会员</label>
                        <div class="layui-input-inline">
                            <select name="member_id" lay-verify="required" lay-search>
                                <option value="">请选择会员</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">充值金额</label>
                        <div class="layui-input-inline">
                            <input type="number" name="amount" value="" lay-verify="required|number" placeholder="请输入充值金额" autocomplete="off" class="layui-input" min="0" step="0.01">
                        </div>
                        <div class="layui-form-mid layui-word-aux">元</div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">赠送金额</label>
                        <div class="layui-input-inline">
                            <input type="number" name="give_amount" value="0" lay-verify="number" placeholder="请输入赠送金额" autocomplete="off" class="layui-input" min="0" step="0.01">
                        </div>
                        <div class="layui-form-mid layui-word-aux">元</div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">支付方式</label>
                        <div class="layui-input-inline">
                            <select name="payment_method" lay-verify="required">
                                <option value="">请选择支付方式</option>
                                <option value="1">现金</option>
                                <option value="2">微信</option>
                                <option value="3">支付宝</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit lay-filter="recharge-form-submit">立即充值</button>
                        </div>
                    </div>
                </div>

                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>充值记录</legend>
                </fieldset>

                <table id="LAY-recharge-list" lay-filter="LAY-recharge-list"></table>
            </div>
        </div>
    </div>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['table', 'form', 'layer'], function(){
        var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        ,layer = layui.layer;
        
        // 加载会员列表
        $.get('/app/ky_shouyin/member/list', function(res){
            if(res.code === 0){
                var html = '<option value="">请选择会员</option>';
                layui.each(res.data, function(index, item){
                    html += '<option value="'+ item.id +'">'+ item.name +' ('+ item.phone +')</option>';
                });
                $('select[name="member_id"]').html(html);
                form.render('select');
            }
        });
        
        // 充值记录列表
        table.render({
            elem: '#LAY-recharge-list'
            ,url: '/app/ky_shouyin/member/rechargeLog'
            ,cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true}
                ,{field: 'member_name', title: '会员姓名', width: 100}
                ,{field: 'member_phone', title: '手机号', width: 120}
                ,{field: 'amount', title: '充值金额', width: 100}
                ,{field: 'give_amount', title: '赠送金额', width: 100}
                ,{field: 'payment_method', title: '支付方式', width: 100, templet: function(d){
                    var methods = {1:'现金', 2:'微信', 3:'支付宝'};
                    return methods[d.payment_method] || '';
                }}
                ,{field: 'store_name', title: '门店', width: 120}
                ,{field: 'staff_name', title: '操作员', width: 100}
                ,{field: 'remark', title: '备注'}
                ,{field: 'created_at', title: '充值时间', width: 160}
            ]]
            ,page: true
            ,limit: 15
            ,text: {
                none: '暂无数据'
            }
        });
        
        //监听提交
        form.on('submit(recharge-form-submit)', function(data){
            var field = data.field;
            
            //提交充值
            $.ajax({
                url: '/app/ky_shouyin/member/doRecharge'
                ,type: 'POST'
                ,data: field
                ,success: function(res){
                    if(res.code === 0){
                        layer.msg('充值成功', {icon: 1});
                        // 重置表单
                        form.val('recharge-form', {
                            'amount': '',
                            'give_amount': '0',
                            'payment_method': '',
                            'remark': ''
                        });
                        // 刷新列表
                        table.reload('LAY-recharge-list');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
            
            return false;
        });
    });
    </script>
</body>
</html> 